import { compileTpFunction } from "./compiler/compiler";
import { mountComponent } from "./lifecycle";
import { initState } from "./state";
//给VUe增加init方法
export function initMixin(Vue) {
  //初始化
  Vue.prototype._init = function (options) {
    const vm = this;
    vm.$options = options; //将用户的选项挂载到实例上
    initState(vm);

    if (options.el) {
      vm.$mount(options.el); //实现数据的挂载
    }
  };

  Vue.prototype.$mount = function (el) {
    const vm = this;
    el = document.querySelector(el);
    let ops = vm.$options;
    if (!ops.render) {
      let template;
      if (!ops.template && el) {
        //没有模板但是写了el
        template = el.outerHTML;
      } else {
        if (el) {
          template = ops.template;
        }
      }

      if (template) {
        const render = compileTpFunction(template);
        ops.render = render;
      }
    }
    //组件的挂载
    mountComponent(vm, el);
    // console.log(ops.render);
  };
}
